package org.yn.base.service.impl;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yn.base.service.IFastDfsService;
import org.yn.base.util.FastdfsUtil;
@Service
public class FastDfsServiceImpl implements IFastDfsService {

    @Value("${fastdfs.url}")
    private String fastDfsUrl;

    @Override
    public String upload(MultipartFile file) throws Exception {

        byte[] bytes = file.getBytes(); //文件的内容
        String originalFilename = file.getOriginalFilename();

        // 获取后缀名
        String extName = FilenameUtils.getExtension(originalFilename);

        String uploadPath = FastdfsUtil.upload(bytes, extName);
// 拼接前缀
        return fastDfsUrl+uploadPath;
    }

    @Override
    public void delete(String path) {

        //截取 不要fastDfsUrl
        path = path.substring(fastDfsUrl.length());

        FastdfsUtil.delete(path);
    }
}
